package mi.payment.transaction.revoke;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import mi.payment.R;
import mi.payment.db.dao.PayDetail;
import mi.payment.ui.base.ParentActivity;
import sunmi.payment.L3.L3BroadcastHelper;
import sunmi.payment.L3.L3ErrorCode;

public class RevokeSearchActivity extends ParentActivity {

    private ListView mListView;
    private EditText mEditSearch;
    private LinearLayout mNoDataView;

    private RevokeAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.transaction_revoke_activity_search);
        initView();
        refreshDataView(null);
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initView() {
        mListView = findViewById(R.id.list_view);
        mEditSearch = findViewById(R.id.edit_search);
        mNoDataView = findViewById(R.id.view_no_data);

        mAdapter = new RevokeAdapter(this);
        mListView.setTextFilterEnabled(true);
        mListView.setAdapter(mAdapter);

        findViewById(R.id.iv_back).setOnClickListener(
                v -> onBackPressed()
        );

        mEditSearch.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
        mEditSearch.setOnTouchListener(
                (v, event) -> {
                    // et.getCompoundDrawables()得到一个长度为4的数组，分别表示左右上下四张图片
                    Drawable drawable = mEditSearch.getCompoundDrawables()[2];
                    // 如果右边没有图片，不再处理
                    if (drawable == null) return false;
                    if (event.getAction() != MotionEvent.ACTION_UP) return false;
                    boolean b = event.getX() > mEditSearch.getWidth() - mEditSearch.getPaddingRight() - drawable.getIntrinsicWidth();
                    if (b) {
                        mEditSearch.setText("");
                    }
                    return false;
                }
        );

        // 设置搜索文本监听
        mEditSearch.addTextChangedListener(
                new TextWatcher() {

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        final String voucherNo = s.toString().trim();
                        executeTask(
                                () -> {
                                    final List<PayDetail> list = new ArrayList<>();
                                    if (voucherNo.trim().length() > 0) {
                                        List<PayDetail> tempList = mPayDetailModel.getAllConsumptionDataWithVoucherNo(voucherNo);
                                        list.addAll(tempList);
                                    }
                                    runOnUI(
                                            () -> refreshDataView(list)
                                    );
                                }
                        );
                    }

                }
        );

    }

    @Override
    public void onBackPressed() {
        L3BroadcastHelper.sendFailedBroadcast(mContext, L3ErrorCode.ERROR_TRADE_CANCEL);
        super.onBackPressed();
    }

    private void refreshDataView(List<PayDetail> list) {
        mAdapter.setData(list);
        if (list == null || list.size() == 0) {
            mNoDataView.setVisibility(View.VISIBLE);
            mListView.setVisibility(View.GONE);
        } else {
            mListView.setVisibility(View.VISIBLE);
            mNoDataView.setVisibility(View.GONE);
        }
    }


}